home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / builtin2.zoo / saverest.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  5KB  |  182 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. #ifndef AMIGA
  25. #include <stdio.h>
  26. #include <sys/file.h>
  27. #else
  28. #include <fcntl.h>     /* lattic C header for amiga */
  29. #endif
  30.  
  31. #ifndef AMIGA
  32. #include "../sim.h"
  33. #include "../aux.h"
  34. #else
  35. #include "/sim.h"
  36. #include "/aux.h"
  37. #endif
  38.  
  39. b_SAVE()    /* R1 points to PSC entry for constant giving file name */
  40. {
  41.     register word op1;
  42.     register pw top;
  43.     struct psc_rec *pscptr;
  44.     char fname[256];
  45.  
  46.     op1 = gregc(1);
  47.     deref(op1);
  48.     pscptr = get_str_psc(op1);
  49.     namestring(pscptr, fname);
  50.     if (save(fname) < 0) {
  51.     printf("*** save: cannot write file ***\n");
  52.     Fail0;
  53.     }
  54. }
  55.  
  56. save(filename)
  57. char *filename;
  58. {
  59.     int fd, space, i;
  60.  
  61.     fd = open(filename, (O_WRONLY | O_CREAT), 0644);
  62.     if (fd < 0) return fd;
  63.     else {
  64.     i = 19;            /* "magic no. for saved states */
  65.     write(fd, &i, 4);
  66.     write(fd, &maxpspace, 4);
  67.         write(fd, &maxmem, 4);
  68.         write(fd, &maxtrail, 4);
  69.     write(fd, &pspace, 4);
  70.     write(fd, &memory, 4);
  71.     write(fd, &tstack, 4);
  72.     write(fd, &curr_fence, 4);
  73.         write(fd, &breg, 4);
  74.         write(fd, &ereg, 4);
  75.         write(fd, &hreg, 4);
  76.         write(fd, &trreg, 4);
  77.         write(fd, &hbreg, 4);
  78.         write(fd, &cpreg, 4);
  79.         write(fd, &pcreg, 4);
  80.         write(fd, &nil_sym, 4);
  81.         write(fd, &list_str, 4);
  82.         write(fd, &list_psc, 4);
  83.         write(fd, &comma_psc, 4);
  84.         write(fd, &interrupt_psc, 4);
  85.         write(fd, &trap_vector[0], 4);
  86.         write(fd, &trap_vector[1], 4);
  87.         
  88.         for (i=0;i<10;i++) write(fd, &flags[i], 4);
  89.  
  90.     space = (word)curr_fence - (word)pspace;
  91.     i = write(fd, pspace, space);
  92.     if (i < space) {close(fd);return -1;}
  93.     space = (word)hreg - (word)heap_bottom;
  94.     i = write(fd, heap_bottom, space);
  95.     if (i < space) {close(fd); return -1;}
  96.     space = (word)local_bottom - ((word)ereg-1040);
  97.     i = write(fd, ((word)ereg-1040), space);
  98.     if (i < space) {close(fd); return -1;}
  99.     space = (word)trail_bottom - (word)trreg;
  100.     i = write(fd, trreg, space);
  101.     if (i < space) {close(fd); return -1;}
  102.     close(fd);
  103.     return 1;
  104.     }
  105. }
  106.  
  107. b_RESTORE()     /* R1 points to PSC entry for constant giving file name */
  108. {
  109.     register word op1;
  110.     register pw top;
  111.     int n;
  112.     struct psc_rec *pscptr;
  113.     char fname[256];
  114.  
  115.     op1 = gregc(1); deref(op1);
  116.     pscptr = get_str_psc(op1);
  117.     namestring(pscptr, fname);
  118.     n = restore(fname);
  119.     if ( n == -1 ) 
  120.     quit("*** restore: cannot access file ***\n");
  121.     else if ( n == -2 )
  122.     quit("*** restore: saved state parameters do not match current values ***\n");
  123. }
  124.  
  125. restore(fname)
  126. char *fname;
  127. {
  128.     int fd, space, i;
  129.     word *npspace, *nmemory, *ntrail;
  130.  
  131.     fd = open(fname, O_RDONLY, 0644);
  132.     if (fd < 0) return fd;
  133.     else {
  134.     read(fd, &i, 4);
  135.     if (i != 19) {
  136.         printf("*** restore: file not an SB-Prolog saved state! ***\n");
  137.         return -1;}
  138.     read(fd, &maxpspace, 4);
  139.     read(fd, &maxmem, 4);
  140.     read(fd, &maxtrail, 4);
  141.     read(fd, &npspace, 4);
  142.     read(fd, &nmemory, 4);
  143.     read(fd, &ntrail, 4);
  144.     read(fd, &curr_fence, 4);
  145.     read(fd, &breg, 4);
  146.     read(fd, &ereg, 4);
  147.     read(fd, &hreg, 4);
  148.     read(fd, &trreg, 4);
  149.     read(fd, &hbreg, 4);
  150.     read(fd, &cpreg, 4);
  151.     read(fd, &pcreg, 4);
  152.     read(fd, &nil_sym, 4);
  153.     read(fd, &list_str, 4);
  154.     read(fd, &list_psc, 4);
  155.     read(fd, &comma_psc, 4);
  156.     read(fd, &interrupt_psc, 4);
  157.     read(fd, &trap_vector[0], 4);
  158.     read(fd, &trap_vector[1], 4);
  159.  
  160.     for (i=0;i<10;i++) read(fd, &flags[i], 4);
  161.  
  162.     if (pspace != npspace) return -2;
  163.     if (memory != nmemory) return -2;
  164.     if (tstack != ntrail)  return -2;
  165.     space = (word)curr_fence - (word)pspace;
  166.     i = read(fd, pspace, space);
  167.     if (i != space) return -1;
  168.     space = (word)hreg - (word)heap_bottom;
  169.     i = read(fd, heap_bottom, space);
  170.     if (i != space) return -1;
  171.     space = (word)local_bottom - ((word)ereg-1040); /* AR has at most 256
  172.                                pvars, + misc info */
  173.     i = read(fd, ((word)ereg-1040), space);
  174.     if (i != space) return -1;
  175.     space = (word)trail_bottom - (word)trreg;
  176.     i = read(fd, trreg, space);
  177.     if (i != space) return -1;
  178.     return 1;
  179.     }
  180. }
  181.  
  182.